<template>
	<view>
		<view class="top">
			<view class="left">
				<view class="title">订单信息</view>
				<view class="text">
					<span style="margin-right: 20rpx;">消费订单：<span
							v-if="order_history">{{order_history.order_count}}</span></span>
					总消费：￥<span v-if="order_history">{{(order_history.consumption_price/100).toFixed(2)}}</span>
				</view>
			</view>
			<image class="right" src="/static/images/orderTime.png" mode="aspectFill"></image>
		</view>
		<view class="content">
			<!-- <view class="heade">
				<view :class="{item:true,active:index == 0}" v-for="item,index in order_status_list" :key="item.status">
					<view>{{item.title}}</view>
					<view class="count">{{item.count}}</view>
				</view>
			</view> -->
			<view class="heade">
				<u-tabs :list="order_status_list" :current="current" @change="changeTab"
					:activeStyle="{ color: '#E93323', fontWeight: 'bold',  transform: 'scale(1.05)' }"
					lineColor="#E93323">
				</u-tabs>
			</view>

			<view class="order-list">
				<view class="order-item" v-for="item,index in order_list" :key="item._id">
					<view class="order-head">
						<view class="left" v-if="item.create_time">
							{{vk.pubfn.timeFormat(item.create_time,"yyyy-MM-dd hh:mm:ss")}}
						</view>
						<view class="right">
							{{item.status === 0 ? '待支付' : item.status === 1 ? '待领取' : item.status === 2 ? '已支付' : item.status == 3 ? '待归还' : '未知状态'}}
						</view>
					</view>
					<view class="order-content">
						<view class="info" v-for="product,product_index in item.product_list"
							:key="product.merchandise_id">
							<view class="left">
								<image class="image" :src="product.image" mode="aspectFill"></image>
								<view class="title">{{product.title}}</view>
							</view>
							<view class="right">
								<view>￥ {{product.price}}</view>
								<view>×{{product.number}}</view>
							</view>
						</view>
						<view class="info-bottom">
							共{{item.product_list.length}}件商品，总金额 <span
								class='price'>￥{{(item.amount/100).toFixed(2)}}</span>
						</view>
					</view>
					<view class="order-bottom">
						<view class="btn cancel" v-if="item.status == 0" @click="cancelOrder(item.out_trade_no)">取消订单
						</view>
						<view class="btn pay" v-if="item.status == 0" @click="goPay(item.out_trade_no)">立即付款</view>
						<!-- <view class="btn info" v-if="item.status != 1" @click="goDetails(item.out_trade_no,item.status)">查看详情</view> -->
						<view class="btn info" v-if="item.status == 1" @click="goDetails(item.out_trade_no,item.status)">去领取</view>
						<view class="btn info" v-if="item.status == 2" @click="goDetails(item.out_trade_no,item.status)">去归还</view>
						<view class="btn info" v-if="item.status == 3" @click="goDetails(item.out_trade_no,item.status)">查看详情</view>
					</view>
				</view>
			</view>

			<view v-if="is_load" style="padding: 80rpx 0;">
				<u-loading-icon text="加载中...">

				</u-loading-icon>
			</view>
			<view v-else style="padding: 80rpx 0;text-align: center;font-size: 28rpx;color: #949494;">
				<view v-if="order_list.length <= 0">
					<u-empty mode="data" text="暂无数据" icon="https://cdn.uviewui.com/uview/empty/data.png">
					</u-empty>
				</view>
				<view v-else>
					已全部加载完成
				</view>
			</view>

		</view>
	</view>
</template>

<script>
	export default {
		onLoad(options) {
			console.log('options',options)
			options.status = options.status || 'all'
			if(this.isNumeric(options.status)) options.status = Number(options.status)
			
			this.current_status = options.status
			let cur_index = this.order_status_list.findIndex((item) => item.status == options.status )
			console.log('cur_index',cur_index)
			this.current = cur_index
			this.initData()
			
			uni.$on('refreshData', () => {
				this.initData()
			})
		
		},
		data() {
			return {
				order_history: null,
				is_load: false,
				is_more: true,
				order_page: 1,
				order_total: 5,
				order_list: [],
				current: 0,
				current_status: "all",
				order_status_list: [{
					name: '全部',
					status: 'all',
					badge: {
						value: null,
					}
				}, {
					name: '待付款',
					status: 0,
					badge: {
						value: null,
					}
				}, {
					name: '待领取',
					status: 1,
					badge: {
						value: null,
					}
				}, {
					name: '待归还',
					status: 2,
					badge: {
						value: null,
					}
				}, {
					name: '已归还',
					status: 3,
					badge: {
						value: null,
					}
				}],

			}
		},
		methods: {
			 isNumeric(str) {
			  return /^\d+$/.test(str);
			},
			initData(){
				this.getOrderList(this.current_status)
				this.getAllHistory()
				this.getStatusGroup()
			},
			goDetails(out_trade_no, order_status) {
				uni.navigateTo({
					url: `/pages/order/order_details/order_details?out_trade_no=${out_trade_no}&order_status=${order_status}`
				})
			},
			goPay(out_trade_no) {
				uni.navigateTo({
					url: `/pages/order/order_pay/order_pay?out_trade_no=${out_trade_no}`
				})
			},
			async cancelOrder(out_trade_no) {
				let data = await vk.callFunction({
					url: '/client/pages/order/kh/cancel_order',
					title: '请求中...',
					data: {
						out_trade_no
					},
				});

				this.getOrderList(this.current_status)
				this.getStatusGroup()
			},
			async getStatusGroup() {
				vk.callFunction({
					url: '/client/pages/order/kh/get_status_group',
					success: (res) => {
						console.log('res', res)
						res.data.count_info.forEach((item) => {
							let cur_index = this.order_status_list.findIndex((items) => items
								.status == item.status)
							this.$set(this.order_status_list[cur_index]['badge'], 'value', item
								.count)
						})

					}
				});
			},
			async getAllHistory() {
				vk.callFunction({
					url: '/client/pages/order/kh/get_all_history',
					data: {

					},
					success: (res) => {
						this.order_history = res.data
					}
				})
			},
			changeTab(data) {
				console.log(data)
				if (data.status === this.current_status) return
				this.order_page = 1
				this.is_more = true
				this.current_status = data.status
				this.getOrderList(data.status)
			},
			getOrderList(status) {
				this.is_load = true
				if (this.order_page === 1) {
					this.order_list = []
				}
				vk.callFunction({
					url: '/client/pages/order/kh/get_order_list',
					data: {
						status: status,
						order_page: this.order_page,
						order_total: this.order_total
					},
					success: (res) => {
						if (this.order_page === 1) {

							this.order_list = res.order_list
						} else {
							this.order_list = this.order_list.concat(res.order_list)
						}

						if (res.order_list.length < this.order_total) {
							this.is_more = false
						}
					},
					complete: (res) => {
						this.is_load = false
					}
				})
			}
		},
		onReachBottom() {
			console.log("拉到底了")
			if (this.is_more === false) return
			this.order_page += 1
			this.getOrderList(this.current_status)
		}
	}
</script>

<style lang="less">
	page {
		background-color: #F5F5F5;
	}

	.top {
		background-color: #E93323;

		box-sizing: border-box;
		padding: 30rpx;

		padding-top: 50rpx;
		padding-bottom: 100rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;

		.left {
			color: white;

			.title {
				font-weight: bold;
				font-size: 36rpx;
			}


			.text {
				color: #ebebeb;
				margin-top: 20rpx;
				font-size: 26rpx;
			}
		}

		.right {
			width: 130rpx;
			height: 120rpx;
		}
	}

	.content {
		width: 690rpx;
		margin: auto;
		margin-top: -60rpx;
		padding-bottom: 40rpx;
		box-sizing: border-box;

		.heade {
			box-shadow: 0 0 5rpx rgba(0, 0, 0, 0.2);

			background-color: white;
			border-radius: 10rpx;

			gap: 20rpx;
			flex-wrap: nowrap;
			overflow-x: scroll;

			.count {
				margin-top: 15rpx;
			}

			.item {
				flex: 0 0 auto;
				display: flex;
				width: 100rpx;
				flex-direction: column;
				align-items: center;
				font-size: 26rpx;
				padding-bottom: 40rpx;

				&.active {
					font-weight: bold;
					border-bottom: 2px solid #E93323;
					padding-bottom: 38rpx;
				}
			}


		}

		.order-list {
			.order-item {
				box-shadow: 0 0 5rpx rgba(0, 0, 0, 0.2);
				border-radius: 10rpx;
				background-color: white;
				margin-top: 15rpx;

				.order-head {
					border-bottom: 1px solid #e5e5e5;
					display: flex;
					padding: 30rpx;
					justify-content: space-between;
					font-size: 28rpx;

					.right {
						color: #E93323;
					}
				}

				.order-content {

					padding: 30rpx;

					.info {
						margin-bottom: 20rpx;
						display: flex;
						justify-content: space-between;

						.left {
							display: flex;

							.image {
								width: 120rpx;
								height: 120rpx;
								border-radius: 5rpx;
								margin-right: 20rpx;
							}

							.title {
								display: -webkit-box;
								overflow: hidden; //超出文本隐藏
								text-overflow: ellipsis; ///超出部分省略号显示
								display: -webkit-box; //弹性盒模型
								-webkit-box-orient: vertical; //上下垂直
								-webkit-line-clamp: 2; //自定义行数
								max-width: 300rpx;
								font-size: 28rpx;
								height: 80rpx;
							}
						}

						.right {
							text-align: right;
							font-size: 26rpx;
							color: #a7a7a7;
						}
					}

					.info-bottom {
						margin-top: 20rpx;
						text-align: right;
						font-size: 28rpx;
						padding-bottom: 20rpx;

						.price {
							color: #E93323;
							font-weight: bold;
						}
					}
				}

				.order-bottom {
					border-top: 1px solid #e5e5e5;
					display: flex;
					align-items: center;
					justify-content: flex-end;
					padding: 30rpx;

					.btn {
						font-size: 28rpx;
						margin-left: 20rpx;
						width: 175rpx;
						text-align: center;
						background-color: #E93323;
						padding: 15rpx 0;
						color: white;
						border-radius: 80rpx;

					}

					.cancel {
						background-color: white;
						border: 1px solid #cccccc;
						color: #cccccc;
					}
				}
			}

		}
	}
</style>